package cn.edu.hpu.jie.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import cn.edu.hpu.jie.entity.VoteInfo;
import cn.edu.hpu.jie.services.VoteService;
import cn.edu.hpu.jie.util.DateUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class VoteAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private VoteService vs = new VoteService();
	private Map<String, Object> app = ActionContext.getContext().getApplication();
	private String id;
	private String pwd;
	private String vote;
	
	@SuppressWarnings("unchecked")
	public String studentVote() throws IOException {
		//检查
		int key = checkVote();
		if(key == 0) {
			return null;
		}
		
		int lines = vs.vote(key, vote);
		if(lines == 0) {
			sendMsg("-4");
			return null;
		}
		
		List<String> vote = (List<String>) app.get("vote");
		vote.add(id);
		sendMsg("1");
		return null;
	}
	
	@SuppressWarnings("unchecked")
	private int checkVote() throws IOException {
		List<String> vote;
		VoteInfo vi;
		if(app.containsKey("vote")) {
			vote = (List<String>) app.get("vote");
			
			if(vote.contains(id)) {
				sendMsg("-1");
				return 0;
			}
			
			vi = getVoteInfo();
			if("-2".equals(checkIP(vi.getIpaddress()))) {
				sendMsg("-2");
				return 0;
			}
		} else {
			vote = vs.getNoAll();
			app.put("vote", vote);
			
			if(vote.contains(id)) {
				sendMsg("-1");
				return 0;
			}
			vi = getVoteInfo();
			if("-2".equals(checkIP(vi.getIpaddress()))) {
				sendMsg("-2");
				return 0;
			}
		}
		
		int key = vs.insertVoteInfo(vi);
		
		if(key == 0) {
			sendMsg("-4");
			return 0;
		}
		
		return key;
	}
	
	private VoteInfo getVoteInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		VoteInfo vi = new VoteInfo();
		vi.setIpaddress(request.getRemoteAddr());
		vi.setNo(id);
		vi.setTime(DateUtil.getNow());
		
		return vi;
	}
	
	@SuppressWarnings("unchecked")
	private String checkIP(String ip) {
		String msg;
		Map<String, Long> ipMap;
		if(!app.containsKey("ipMap")) {
			ipMap = new HashMap<String, Long>();
			app.put("ipMap", ipMap);
		}
		ipMap = (Map<String, Long>) app.get("ipMap");
		if(ipMap.containsKey(ip) && (System.currentTimeMillis() - ipMap.get(ip)) < 10000) {
			msg = "-2";
		} else {
			ipMap.put(ip, System.currentTimeMillis());
			msg = "1";
		}
		
		if(ipMap.size() >= 200) {
			Iterator<String> it = ipMap.keySet().iterator();
			while(it.hasNext()) {
				if((System.currentTimeMillis() - ipMap.get(it.next())) > 10000) {
					it.remove();
				}
			}
		}
		return msg;
	}
	
	private void sendMsg(String msg) throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println(msg);
		out.flush();
		out.close();
	}
	
	//set & get
	public void setId(String id) {
		this.id = id;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public void setVote(String vote) {
		this.vote = vote;
	}
}
